<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>login</title>
    <link rel="stylesheet" href="css/data.css">
    <style>

    </style>
</head>
<body>

<form action="index.php" method="post" name="loginForm" class="loginForm">
    <fieldset>
        <legend>登录</legend>
        <div class="itm">
            <input type="text" id="username" name="username" placeholder="用户名"/>
        </div>
        <div class="itm">
            <input type="password" id="paw" name="pwd" placeholder="密码"/>
        </div>
        <div class="itm">
            <button type="submit" name="submit">登录</button>
            <a href="php/reg.php">注册</a>
        </div>
    </fieldset>


    <script>
        check();

        function check() {
            var form = document.loginForm;
            form.submit.onclick = function () {
                if (form.username.value === "" || form.pwd.value === "") {
                    alert("用户名或密码不能为空");
                    return false;
                } else {
                    return true;
                }
            }
        }

    </script>
</body>
</html>


<?php
/**
 * Created by PhpStorm.
 * User: Office
 * Date: 2017/12/29
 * Time: 16:47
 */
header("content-type:text/html;charset=utf-8");
session_start();

if (!empty($_POST)) {
    login();
}

function login()
{
    if ($_POST['username'] === "" || $_POST['pwd'] === "") {
        echo "<div style='color: red;margin-top: 20px;'>用户名或者密码不能为空</div>";
        exit();
    }
    $link = mysqli_connect('localhost', 'root', 'root', "test");
    if (!$link) {
        exit("连接失败");
    }

    $fuser = $_POST['username'];
    $fpwd = $_POST['pwd'];
    $pwdL = md5($fpwd);
    $sql = "select * from user WHERE username = '$fuser'and pwd = '$pwdL'";
    $result = mysqli_query($link, $sql);
    $data = mysqli_fetch_array($result);
    if ($data) {
        $_SESSION['user'] = $fuser;
        header("location:php/pagingData.php");
    } else {
        echo "<strong style='color: red'>用户名或者密码不正确！</strong>";
    }


}

?>